@extends('web.frame_layout')
<link rel="stylesheet" href="{{asset('web/css/my-address.css')}}">
<?php $parent_menu = "购物商城"; $menu_selected = "我的收货地址";?>
@section('content')
    <div class="m-right-nr">
        <div>
            <h2><span>我的收货地址</span></h2>
            <div class="m-vip_up">
                <form id="my_address" action="{{route('web.address.save')}}" method="post" autocomplete="on">
                    <ul>
                        <li>
                            <label for="name" class="hidden-xs"><i>*</i>收货人姓名：</label>
                            <input type="text" id="name" name="name" value="{{$my_addr->name or ""}}"
                                   placeholder="请输入收货人姓名">
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="tel" class="hidden-xs"><i>*</i>手机号：</label>
                            <input type="text" id="tel" name="tel" value="{{$my_addr->tel or ""}}" placeholder="请输入手机号">
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label class="hidden-xs"><i>*</i>选择地区：</label>
                            <select name="province" id="province">
                                <option value="">==省==</option>
                                @foreach($provinces as $province)

                                    <option value="{{$province ->id}}"

                                            @if($my_addr && $my_addr->province == $province ->id)
                                            selected
                                            @endif>
                                        {{$province->name}}
                                    </option>
                                @endforeach
                            </select>
                            <select name="city" id="city">
                                @if($my_addr && $my_addr->city)
                                    <option value="{{$my_addr->city}}">{{getAreaById($my_addr->city)}}</option>
                                @else
                                    <option value="">==市==</option>
                                @endif
                            </select>
                            <select name="county" id="county">
                                @if($my_addr && $my_addr->city)
                                    <option value="{{$my_addr->county}}">{{getAreaById($my_addr->county)}}</option>
                                @else
                                    <option value="">==县==</option>
                                @endif
                            </select>
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <label for="addr" class="hidden-xs"><i>*</i>详细街道地址：</label>
                            <input type="text" id="addr" name="addr" value="{{$my_addr->addr or ""}}"
                                   placeholder="平安街橄榄城7单元7号">
                            <p class="u-ts"></p>
                        </li>
                        <li>
                            <button>立即保存</button>
                        </li>
                    </ul>
                </form>
            </div>
        </div>
    </div>
@endsection

@section('footer')
    <script type="text/javascript">


        $("#province").change(function () {
            var province = $("#province").val();
            var url = "/web/address_change";

            if (province == '') {
                $("#city").html("<option value=''>==市==</option>");
                $("#county").html("<option value=''>==县/区==</option>");
            } else {
                $.get(url, {id: province}, function (data) {
                    $("#city").html("<option value=''>==市==</option>");
                    $("#county").html("<option value=''>==县/区==</option>");
                    $("#city").append(data);
                }, "text");
            }
        });
        $("#city").change(function () {
            var id = $(this).val();
            var url = "/web/address_change";
            if (id == '') {
                $("#county").html("<option value=''>==县/区==</option>");
            }
            else {
                $.get(url, {id: id}, function (data) {
                    $("#county").html("<option value=''>==县/区==</option>");
                    $("#county").append(data);
                }, "text");

            }

        });

        $("#my_address").validate({
            rules: {
                name: {
                    required: true,
                    minlength: 2,
                    maxlength: 10
                },
                tel: {
                    required: true,
                    maxlength: 20
                },
                addr: {
                    required: true,
                    maxlength: 255
                }
            }, errorPlacement: function (error, element) {
                $(element).siblings('.u-ts').html(error)
            },
            errorElement: 'span',
            submitHandler: function (form) {
                $(form).ajaxSubmit({
                    dataType: "json",
                    type: "post",
                    success: function (data) {
                        if (data.code == 0) {
                            toastr["success"](data.msg);
                            setInterval(function () {
                                window.location.reload();
                            }, 2000);
                        } else {
                            toastr["error"](data.msg);
                        }
                    }
                });
            }
        });
    </script>
@endsection